﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System.Collections.Generic;

namespace SlamDunk.MES.Domain.Entities.FoundationContext
{
    /// <summary>
    /// 计量单位
    /// </summary>
    public class MeasureUnit : PlainEntity, INumerable, INameable, ISortable
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected MeasureUnit() { }
        #endregion

        #region 01.创建计量单位构造器
        /// <summary>
        /// 创建计量单位构造器
        /// </summary>
        /// <param name="unitNo">单位编号</param>
        /// <param name="unitName">单位名称</param>
        /// <param name="masterCoefficient">主计量单位转换系数</param>
        /// <param name="isMaster">是否主计量单位</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public MeasureUnit(string unitNo, string unitName, decimal? masterCoefficient, bool isMaster, int sort, string description)
            : this()
        {
            this.Number = unitNo;
            this.Name = unitName;
            this.MasterCoefficient = isMaster ? 1 : masterCoefficient;
            this.IsMaster = isMaster;
            this.Sort = sort;
            this.Description = description;
        }
        #endregion

        #endregion

        #region # 属性

        #region 单位编号 —— string Number
        /// <summary>
        /// 单位编号
        /// </summary>
        public string Number { get; private set; }
        #endregion

        #region 单位名称 —— string Name
        /// <summary>
        /// 单位名称
        /// </summary>
        public string Name { get; private set; }
        #endregion

        #region 主计量单位转换系数 —— decimal? MasterCoefficient
        /// <summary>
        /// 主计量单位转换系数
        /// </summary>
        /// <remarks>
        /// 当前计量单位 * 主计量单位转换系数 = 主计量单位
        /// </remarks>
        public decimal? MasterCoefficient { get; private set; }
        #endregion

        #region 是否主计量单位 —— bool IsMaster
        /// <summary>
        /// 是否主计量单位
        /// </summary>
        public bool IsMaster { get; private set; }
        #endregion

        #region 排序 —— int Sort
        /// <summary>
        /// 排序
        /// </summary>
        public int Sort { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 计量单位转换系数字典 —— {string, decimal?} Coefficients
        /// <summary>
        /// 只读属性 - 计量单位转换系数字典
        /// </summary>
        /// <remarks>
        /// 键：计量单位编号，值：转换系数
        /// 当前计量单位 * 值 = 目标计量单位
        /// </remarks>
        public IDictionary<string, decimal?> Coefficients
        {
            get
            {
                IDictionary<string, decimal?> coefficients = new Dictionary<string, decimal?>();
                foreach (MeasureUnit measureUnit in this.MeasureUnitGroup.MeasureUnits)
                {
                    decimal? coefficient = this.MasterCoefficient / measureUnit.MasterCoefficient;
                    coefficients.Add(measureUnit.Number, coefficient?.Revise());
                }

                return coefficients;
            }
        }
        #endregion

        #region 导航属性 - 计量单位组 —— MeasureUnitGroup MeasureUnitGroup
        /// <summary>
        /// 导航属性 - 计量单位组
        /// </summary>
        public virtual MeasureUnitGroup MeasureUnitGroup { get; internal set; }
        #endregion

        #endregion
    }
}
